package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.*;
import com.example.shoppingmall.service.AdvertisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/Advertising")
public class AdvertisController {
    @Autowired
    AdvertisService advertisService;

    //广告列表：分页显示+条件查询
    @RequestMapping("/Advertisinglist")
    public String Advertisinglist(@RequestParam(value = "Advertisementname",required = false) String Advertisementname,
                                  @RequestParam(value = "utime",required = false) String utime, @RequestParam(value = "pageno",defaultValue = "1") String pageno,
                                  @RequestParam(value = "pageSize",defaultValue = "6") String pageSize, Model model){
        int count=advertisService.advertisingcount(Advertisementname,utime);
        int num=(Integer.valueOf(pageno)-1)*Integer.valueOf(pageSize);
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageCount(count);
        List<Advertising> advertisingList=advertisService.getAdvertisingAll(Advertisementname,utime,num,Integer.valueOf(pageSize));
        model.addAttribute("page",page);
        model.addAttribute("advertisingList",advertisingList);
        model.addAttribute("Advertisementname",Advertisementname);
        model.addAttribute("utime",utime);
        return "Operate/Advertisementlist";
    }

    //删除广告信息
    @MyLog(value = "运营管理->广告列表->删除")
    @RequestMapping("/delAdvertising")
    @ResponseBody
    public Map<Object,Object> delAdvertising(Integer id){
        Map<Object,Object> map=new HashMap<>();
        if(advertisService.delAdvertising(id)>0){
            map.put("del","yes");
        }else{
            map.put("del","no");
        }
        return map;
    }

    //跳转添加广告页面
    @RequestMapping("/AdvertisementAdd")
    public String AdvertisementAdd(Model model){
        //遍历广告分类
        List<Advertisingtype> atype=advertisService.getAdvertisingtype();
        //遍历所属广告位
        List<Advertisingspace> aspace=advertisService.getAdvertisingspace();
        //查询所有商品
        List<Commodity> commodityList=advertisService.getCommoditylist();
        //查询所有文章
        List<Articlelist> articlelistList=advertisService.getArticlelistall();
        //查询所有文章分类
        List<Article> articletype=advertisService.getArticletypes();

        model.addAttribute("atype",atype);
        model.addAttribute("aspace",aspace);
        model.addAttribute("commodityList",commodityList);
        model.addAttribute("articlelistList",articlelistList);
        model.addAttribute("articletype",articletype);
        return "Operate/AdvertisementAdd";
    }

    //添加广告信息
    @MyLog(value = "运营管理->广告列表->添加")
    @RequestMapping("/addAdvertising")
    public String addAdvertising(@RequestParam("aspace") String aspace, @RequestParam("aname") String aname,
                                 @RequestParam("atype") String atype, @RequestParam(value = "URLlink",required = false) String URLlink,
                                 @RequestParam(value = "shop",required = false) String shop, @RequestParam(value = "article",required = false) String article,
                                 @RequestParam(value = "articleType",required = false) String articleType, @RequestParam("asort") String asort,
                                 @RequestParam("aaddress") String aaddress){
        Advertising advertising=new Advertising();
        advertising.setAspace(Integer.valueOf(aspace));
        advertising.setAname(aname);
        advertising.setAtype(Integer.valueOf(atype));
        advertising.setURLlink(URLlink);
        advertising.setShop(Integer.valueOf(shop));
        advertising.setArticle(Integer.valueOf(article));
        advertising.setArticleType(Integer.valueOf(articleType));
        advertising.setAsort(Integer.valueOf(asort));
        advertising.setAaddress(aaddress);
        if(advertisService.addAdvertising(advertising)>0){
            return "redirect:/Advertising/Advertisinglist";
        }
        return "error";
    }

    //查询广告详情
    @MyLog(value = "运营管理->广告列表->查询详情")
    @RequestMapping("/selAdvertising")
    public String selAdvertising(Integer id,Model model){
        Advertising advertising=advertisService.selAdvertising(id);
        model.addAttribute("advertisings",advertising);
        //遍历广告分类
        List<Advertisingtype> atype=advertisService.getAdvertisingtype();
        //遍历所属广告位
        List<Advertisingspace> aspace=advertisService.getAdvertisingspace();
        //查询所有商品
        List<Commodity> commodityList=advertisService.getCommoditylist();
        //查询所有文章
        List<Articlelist> articlelistList=advertisService.getArticlelistall();
        //查询所有文章分类
        List<Article> articletype=advertisService.getArticletypes();

        model.addAttribute("atypes",atype);
        model.addAttribute("aspaces",aspace);
        model.addAttribute("commodityLists",commodityList);
        model.addAttribute("articlelistLists",articlelistList);
        model.addAttribute("articletypes",articletype);
        return "Operate/updAdvertis";
    }

    //编辑广告信息
    @MyLog(value = "运营管理->广告列表->编辑")
    @RequestMapping("/updAdvertising")
    public String updAdvertising(@RequestParam("aspace") String aspace, @RequestParam("aname") String aname,
                                 @RequestParam("atype") String atype, @RequestParam(value = "URLlink",required = false) String URLlink,
                                 @RequestParam(value = "shop",required = false) String shop, @RequestParam(value = "article",required = false) String article,
                                 @RequestParam(value = "articleType",required = false) String articleType, @RequestParam("asort") String asort,
                                 @RequestParam("aaddress") String aaddress,@RequestParam("id") String id){

            Advertising advertising = new Advertising();
            advertising.setAspace(Integer.valueOf(aspace));
            advertising.setAname(aname);
            advertising.setAtype(Integer.valueOf(atype));
            advertising.setURLlink(URLlink);
            advertising.setShop(Integer.valueOf(shop));
            advertising.setArticle(Integer.valueOf(article));
            advertising.setArticleType(Integer.valueOf(articleType));
            advertising.setAsort(Integer.valueOf(asort));
            advertising.setAaddress(aaddress);
            advertising.setId(Integer.valueOf(id));
        if(advertisService.updAdvertising(advertising)>0){
            return "redirect:/Advertising/Advertisinglist";
        }
        return "error";
    }
}
